package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.BusNeighborhoodLedger;
import com.ruoyi.business.service.IBusNeighborhoodLedgerService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BusBldgMapper;
import com.ruoyi.business.domain.BusBldg;
import com.ruoyi.business.service.IBusBldgService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 楼栋管理Service业务层处理
 * 
 * @author ling
 * @date 2024-07-12
 */
@Service
public class BusBldgServiceImpl implements IBusBldgService 
{
    @Resource
    private BusBldgMapper busBldgMapper;

    @Autowired
    private IBusNeighborhoodLedgerService busNeighborhoodLedgerService;
    /**
     * 查询楼栋管理
     * 
     * @param id 楼栋管理主键
     * @return 楼栋管理
     */
    @Override
    public BusBldg selectBusBldgById(Long id)
    {
        return busBldgMapper.selectBusBldgById(id);
    }

    /**
     * 查询楼栋管理列表
     * 
     * @param busBldg 楼栋管理
     * @return 楼栋管理
     */
    @Override
    public List<BusBldg> selectBusBldgList(BusBldg busBldg)
    {
        return busBldgMapper.selectBusBldgList(busBldg);
    }

    /**
     * 新增楼栋管理
     * 
     * @param busBldg 楼栋管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBusBldg(BusBldg busBldg)
    {
        busBldg.setCreateUser(SecurityUtils.getUserId());
        busBldg.setCreateBy(SecurityUtils.getUsername());
        busBldg.setCreateTime(DateUtils.getNowDate());
        busBldg.setUpdateUser(SecurityUtils.getUserId());
        busBldg.setUpdateBy(SecurityUtils.getUsername());
        busBldg.setUpdateTime(DateUtils.getNowDate());

        int rows = busBldgMapper.insertBusBldg(busBldg);
        //        获取小区id
        Long neighborId = busBldg.getNeighborId();
        updateBusBldgCount(neighborId);
        return rows;
    }

    /**
     * 修改楼栋管理
     * 
     * @param busBldg 楼栋管理
     * @return 结果
     */

    @Override
    public int updateBusBldg(BusBldg busBldg)
    {
        busBldg.setUpdateUser(SecurityUtils.getUserId());
        busBldg.setUpdateBy(SecurityUtils.getUsername());
        busBldg.setUpdateTime(DateUtils.getNowDate());
        return busBldgMapper.updateBusBldg(busBldg);
    }

    /**
     * 批量删除楼栋管理
     * 
     * @param ids 需要删除的楼栋管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusBldgByIds(Long[] ids)
    {

//        for (Long id : ids){
//            System.out.println(id);
//        }
//        要在删除之前查询，不然找不到数据
        BusBldg busBldg = busBldgMapper.selectBusBldgById(ids[0]);

        int rows = busBldgMapper.deleteBusBldgByIds(ids);
//        获取数组元素id
        Long neighborId = busBldg.getNeighborId();
        updateBusBldgCount(neighborId);
        return rows;
    }

    /**
     * 删除楼栋管理信息
     * 
     * @param id 楼栋管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBusBldgById(Long id)
    {

        int rows = busBldgMapper.deleteBusBldgById(id);
        //        获取小区id
        BusBldg busBldg = busBldgMapper.selectBusBldgById(id);
        Long neighborId = busBldg.getNeighborId();
        updateBusBldgCount(neighborId);
        return rows;
    }

    /**
     * 更新小区的楼栋数
     *
     * @param neighborId 小区id
     */
    public void updateBusBldgCount(Long neighborId){
        //        查询小区的楼栋数
        if (StringUtils.isNotNull(neighborId)){
            Long count = busBldgMapper.getCount(neighborId);
            BusNeighborhoodLedger busNeighborhoodLedger = new BusNeighborhoodLedger(neighborId, count);
//        更新小区楼栋数
            busNeighborhoodLedgerService.updateBusNeighborhoodLedger(busNeighborhoodLedger);
        }
    }
}
